<?php
/**
 * @Author: rocky
 * Date: 2019/9/28
 * Time: 11:22
 */


namespace app\shop\controller\api\v1;

use app\api\service\WechatMiniService;
use app\common\controller\BaseApiController;
use app\common\model\Account;
use app\common\model\ShopMember;
use app\common\model\ShopOrderTip;
use app\common\model\ShopUserMoneyRecord;
use app\shop\model\ShopOrder;
use app\shop\model\ShopOrderGoods;
use think\Db;
use think\Exception;

/**
 * 定时刷新任务
 * Class Recharge
 * @package app\shop\controller\api\v1
 */
class Time extends BaseApiController
{
    /**
     * 预约订单自动过期
     */
    public function  getOverdue()
    {
        $order_list = ShopOrder::where('status', 2)->where('type',1)->whereTime('subscribe_time', '<=', time())->select();
        if ($order_list) {
            Db::startTrans();
            try {
                foreach ($order_list as $key => $value){
                    if ($value['pay_type'] == 1){
                        $res = WechatMiniService::refund($value['pay_no'],$value['order_no'],$value['total_price'],$value['total_price']);
                        if ($res) {
                            ShopOrder::where('id', $value['id'])->setField('status', -1);
                            Account::create([
                                'uid' => $value['uid'],
                                'title' => "{$value['title']}",
                                'desc' => "退款微信钱包 - 订单号：{$value['order_no']}",
                                'money' => $value['price'],
                                'type' => 2,
                                'store_id' => $value['store_id'],
                                'order_id' => $value['id']                            ]);
                            $data = [
                                'order_id' => $value['id'],
                                'uid' => $value['uid'],
                                'title' => '退款成功',
                                'content' => "¥{$value['total_price']}已成功退回到支付账户"
                            ];
                        }else {
                            Db::rollback();
                            $this->errorCode(100,'失败1001');
                        }
                    }
                    elseif ($value['pay_type'] == 3){
                        ShopOrder::where('id', $value['id'])->setField('status', -1);
                        Account::create([
                            'uid' => $value['uid'],
                            'title' => "{$value['title']}",
                            'desc' => "退款余额钱包 - 订单号：{$value['order_no']}",
                            'money' => $value['price'],
                            'type' => 2,
                            'store_id' => $value['store_id'],
                            'order_id' => $value['id']
                        ]);
                        ShopUserMoneyRecord::record($value['total_price'],$value['uid'],"预约退款",1,true,"余额支付 - 订单号：{$value['order_no']}");
                        $data = [
                            'order_id' => $value['id'],
                            'uid' => $value['uid'],
                            'title' => '退款成功',
                            'content' => "¥{$value['total_price']}已成功退回到账户余额"
                        ];
                    }
                    else{
                        $buy_num = ShopOrderGoods::where('order_id',$value['id'])->sum('num');
                        $rs =ShopMember::where('uid', $value['uid'])->where('status',1)->setDec('use_num', $buy_num);
                        $data = [
                            'order_id' => $value['id'],
                            'uid' => $value['uid'],
                            'title' => '审核成功',
                            'content' => "总{$buy_num}件数已成功退回到账户件数"
                        ];
                    }

                    ShopOrder::where('id',$value['id'])->update(['status' => 6]);
                }
                ShopOrderTip::create($data);
                Db::commit();
            } catch (Exception $e) {
                Db::rollback();
                $this->errorCode(100,$e->getMessage());
            }
        }
        $this->successCode();
    }
}